import util from '../../../utils/index.js';
import { userModel, infoModel } from '../../../apis/index.js';
var amapFile = require('../../../utils/amap-wx.js');
import config from '../../../config.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabList:[{
      id:6,
      title:"圈子"
    },{
      id:3,
      title:"求合作"
    }, {
      id:4,
      title: "产品推广"
    }, {
    id: 2,
    title: "活动"
    },{
      id: 2,
      title: "招聘"
    }, {
      id: 2,
      title: "优惠发布"
    }],
    tabList2:[{
      id:1,
      title:"最新"
    },{
        id: 1,
        title: "最近"
      }, {
        id: 1,
        title: "最热"
      }],
    tabActiveIndex:0,
    param: {
      page: 1,
      keyword:'',
      longitude: 104.043243,
      latitude: 30.641904,
      order_type:6
    },
    list:{
      data:[]
    },
    refresh: false,
    loading: true,
    showAlertAd:false,
    scrollTop: 0,
    current:0,
    swiperCategory: config.swiperCategory,
    swiperAd: config.swiperAd,
    roller: config.roller,
    tabbar: config.tabbar,
    notice: config.notice,
    location:config.location,
    blockList:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    util.showLoading()
    this.firstLoad();  
  },
  //首次渲染
  firstLoad: function () {
    let that=this;
    that.initPageConfig(); 
    
    getApp().getLocationInfo().then((d)=>{
      console.log(d)
      let { longitude, latitude,city } = d;
      that.setData({
        'param.longitude': longitude,
        'param.latitude': latitude,
        city
      })
      that.getList();
    })
   
  },
  //初始化page页面配置
  initPageConfig: function () {
    let that = this;
    let showAlertAd = getApp().globalData.showAlertAd
    Promise.all([infoModel.getIndex(),getApp().getConfigInfo(),]).then((d)=>{
      util.hideAll();
      let otherData=d[0].data;
      let configInfo=d[1]
      let { swiperCategory, swiperAd, tabbar, roller } = configInfo
      let searchTitle = configInfo.searchTitle.info;
      that.setData({
        swiperCategory,
        swiperAd,
        roller,
        tabbar,
        searchTitle,
        blockList: configInfo.extraConfig.info_panel,
        panel_type: configInfo.extraConfig.panel_type,
        openIndexTop: configInfo.extraConfig.infotop,
        otherData,
        showAlertAd
      })
    })
  },
  //tab切换
  tabChange(e) {

    let that = this;
    let index = e.detail.index;
    let {tabList} = this.data;
    
    // 切换tab后改变活动tab索引,以及初始化页面page
    that.setData({
      tabActiveIndex: index,
      loading: true,
      'list.data': [],
      'param.page': 1,
      'param.order_type': tabList[index].id
    }, function () {
      that.getList();
    })

  },
  //获取列表
  getList: function () {

    let that = this;
    let { refresh, param, list, tabActiveIndex } = this.data;

    infoModel.getList(param).then((d) => {
      let oldlist = list;
      let newlist = d.data;
     
      //如果刷新,则不加载老数据
      if (!refresh) {
        newlist.data = [...oldlist.data, ...newlist.data];
      }
      that.setData({
        list: newlist,
        loading: false,
        refresh: false,
      })
    })

  },
  //刷新
  onPullDownRefresh: function () {
    let that = this;
    that.setData({
      refresh: true,
      'param.page': 1
    }, function () {
      wx.showNavigationBarLoading()
      that.firstLoad();
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this;
    let { loading, list } = this.data;
    let { current_page, last_page } = list;
    if (current_page != last_page && !loading) {
      that.setData({
        'param.page': current_page + 1,
        loading: true
      }, function () {
        that.getList();
      })
    }
  },
  search: function () {
    let that = this;

    that.setData({
      loading: true,
      'param.page': 1,
      list: {
        data: []
      },
    }, function () {
      that.getList()
    })
  },
  goUrl(e) {
    util.goUrl(e)
  },
  onPageScroll(e) {
    this.setData({
      scrollTop: e.scrollTop
    })
  },
  categoryChange(e) {
    let current = e.detail.current;
    this.setData({
      current
    })
  },
  previewImage: function (e) {
    let { index, src } = util.getData(e);
    let { list} = this.data;
    let imageList = list.data[index].img;
    let imgArr = [];
    for (let i in imageList) {
      imgArr.push(imageList[i].path)
    }
    wx.previewImage({
      current: src,
      urls: imgArr,
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  setParam: function (e) {
    let { key } = util.getData(e);
    let value = util.getValue(e);
    key = `param.${key}`;

    this.setData({
      [key]: value
    })
  },
  
  like: function (e) {
    let that = this;
    let { index } = util.getData(e);

    let id = this.data.list.data[index].id;
    let like = this.data.list.data[index].like;
    let key = `list.data[${index}].like`;
    let key1 = `list.data[${index}].is_like`
    util.showLoading()
    infoModel.like({ info_id:id }).then((d) => {
      util.hideAll()
      util.showSuccess('点赞成功');
      that.setData({
        [key]: like + 1,
        [key1]:true
      })
    })
  },
  closeMask:function(){
    getApp().globalData.showAlertAd=false;
    this.setData({
      showAlertAd:false
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})